<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        body {
            background: #000;
            text-align: center
        }

        #c1 {
            background: #fff;
        }
    </style>

</head>

<body>
    <input type="color" id="color1">
    <canvas id="c1" width="800" height="600">

    </canvas>
</body>
<script>
    let oC = document.getElementById('c1');
    let gd = oC.getContext('2d');       //图形上下文-绘图用的接口
    let lastX, lastY;
    let oColor=document.getElementById('color1');

    let color='black';
    oColor.onchange=function(){
        color=this.value
    }
    oC.onmousedown = function (ev) {
        // gd.moveTo(ev.pageX, ev.pageY)   //page计算的是整个页面
        lastX=ev.offsetX
        lastY=ev.offsetY
        oC.onmousemove = function (ev) {
            gd.moveTo(lastX, lastY)
            lastX=ev.offsetX
            lastY=ev.offsetY
            gd.lineTo(ev.offsetX, ev.offsetY)
            gd.strokeStyle=color
            gd.stroke()
            gd.beginPath()
        }
        
    }

   
    oC.onmouseup = function () {
        oC.onmousemove=null
    }


</script>

</html>